<?php


namespace App\Services;


use App\Models\AdminUser;
use App\Models\Department;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;

class AdminUserService
{
    protected $role_type = [0 => '院职工', 2 => '科室领导', 1 => '院级领导'];

    public function getGridColumn(Grid $grid)
    {
        $roles = $this->role_type;
        $user = Admin::user();
        $grid->model()->where('id', '>', '1');
        $grid->column('username', trans('用户名/工号'));
        $grid->column('name', '姓名');
        $grid->column('role_type', trans('角色'))->using($roles)->label(['success', 'primary', 'info']);
        $grid->column('mobile', trans('电话'));
        $grid->column('department.name', __('Department'));
        $grid->column('education', '学历');
        $grid->column('academic_title', '职称');
        $states = [
            'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '否', 'color' => 'primary']
        ];
        $grid->column('enabled', trans('启用'))->switch($states);
        $grid->column('id_card', '身份证')->hide();
        $grid->column('sex', '性别')->using(['', '男', '女'])->hide();
        $grid->column('created_at', trans('创建时间'))->hide();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
        });
        $grid->batchActions(function (Grid\Tools\BatchActions $actions) {
            $actions->disableDeleteAndHodeSelectAll();
        });
        $grid->disableExport();
//        if($user['role_type'] ==1){
//            $grid->filter(function(Grid\Filter $filter)use($user){
//                $filter->disableIdFilter();
//                $filter->column(1/2,function(Grid\Filter $filter){
//                    $filter->where(function($query){
//                        $query->where('username','like',"%$this->input%")
//                            ->orWhere('name','like',"%$this->input%")
//                            ->orWhere('eno','like',"%$this->input%")
//                            ->orWhere('id_card','like',"%$this->input%")
//                            ->orWhere('education','like',"%$this->input%")
//                            ->orWhere('academic_title','like',"%$this->input%")
//                            ->orWhere('mobile','like',"%$this->input%");
//                    },'关键字');
//                });
//                $filter->column(1/2,function(Grid\Filter $filter){
//                    $filter->equal('role_type','角色')->select($this->role_type);
//                });
//            });
//        }else{
//            $grid->quickSearch(['username', 'name', 'mobile','email','id_card','education','academic_title'])->placeholder('关键字');
//            $grid->disableFilter();
//        }
        $grid->quickSearch(function ($query, $value) {
            $query->where(function ($query) use ($value) {
                $query->where('username', 'like', "%$value%")
                    ->orWhere('name', 'like', "%$value%")
                    ->orWhere('mobile', 'like', "%$value%")
                    ->orWhere('email', 'like', "%$value%")
                    ->orWhere('id_card', 'like', "%$value%")
                    ->orWhere('education', 'like', "%$value%")
                    ->orWhere('academic_title', 'like', "%$value%")
                    ->orWhereHas('department', function ($query) use ($value) {
                        $query->where('name', 'like', "%$value%");
                    });
            });
        })->placeholder('关键字');
        //$grid->quickSearch(['username', 'name', 'mobile','email','id_card','education','academic_title','department.name'])->placeholder('关键字');
        $grid->disableFilter();
        if ($user['role_type'] == 1) {
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->select('role_type', '角色', $this->role_type);
            });
        }
        return $grid;
    }


    public function form()
    {
        $user = Admin::user();
        $roles = $this->role_type;
        $form = new Form(new AdminUser());
        $form->column(1 / 2, function (Form $form) use ($user, $roles) {
            $form->text('username', trans('admin.username') . '/工号')
                ->creationRules(['required', "unique:admin_users,username"], ['required' => '账号不能为空', 'unique' => '账号不能重复'])
                ->updateRules(['required', "unique:admin_users,username,{{id}}"], ['required' => '账号不能为空', 'unique' => '账号不能重复']);
            $form->text('name', '姓名')->rules('required');
            $form->password('password', trans('admin.password'))->rules('required|confirmed');
            $form->password('password_confirmation', trans('admin.password_confirmation'))->rules('required')
                ->default(function ($form) {
                    return $form->model()->password;
                });
            if ($user['role_type'] == 1) {
                $form->radio('role_type', '角色')->required()->options($roles)->default(0);
                $form->select('department_id', '所属部门')->options(Department::departmentOptions())->required();
            } elseif ($user['role_type'] == 2 && $form->isCreating()) {
                $form->hidden('role_type', '角色');
                $form->hidden('department_id', '所属部门');
            }
            $form->mobile('mobile', '电话');
        });
        $form->column(1 / 2, function (Form $form) {
            $form->text('id_card', '身份证号/护照号');
            $form->radio('sex', '性别')->options([1 => '男', 2 => '女'])->default(1);
            $form->date('birth', '出生日期')->width('120px');
            $form->text('education', '学历');
            $form->text('academic_title', '职称');
            $states = [
                'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '否', 'color' => 'primary'],
            ];
            $form->switch('enabled', '启用')->states($states)->default(1);
        });
        $form->ignore(['password_confirmation']);

        $form->saving(function (Form $form) use ($user) {
            if ($user['role_type'] == 2 && $form->isCreating()) {
                $form->role_type = 0;
                $form->department_id = $user['department_id'];
            }
            if (request()->ajax() && request()->pjax()) {
                if ($form->password && $form->model()->password != $form->password) {
                    $form->password = bcrypt($form->password);
                }
            }
        });
        return $form;
    }

}
